1868A - Fill in the Matrix - CodeForces Solution


constructive algorithms implementation

Please click on ads to support us..

C++ Code:

// Source: https://usaco.guide/general/io
 
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mex(vector<int> &arr, int N)
{
 
  // sort the array
  sort(arr.begin(), arr.end());
 
  int mex = 0;
  for (int idx = 0; idx < N; idx++)
  {
    if (arr[idx] == mex)
    {
      // Increment mex
      mex += 1;
    }
  }
 
  // Return mex as answer
  return mex;
}
 
int calcbeaut(vector<vector<int>> arr, int coloms, int rows){
	vector<int> mexv;
	for (int i=0; i < coloms; i++){
		vector<int> v;
		for (int j = 0; j < rows; j++){
			v.push_back(arr[j][i]);
		}
		mexv.push_back(mex(v, v.size()));
	}
	for (int i = 0; i < mexv.size(); i++){
		//cout << mexv[i] << "\n";
	}
	return mex(mexv, mexv.size());
}
 
signed main() {
	int t;
	cin >> t;
	while (t--){
		int n,m;
		cin >> n >> m;
		int dupn = n;
		n = min(n,m);
		vector<vector<int>> ans;
		ans.push_back({});
 
		for (int i = 0; i< dupn; i++){
			ans.push_back({});
			for (int j = 0; j < m; j++){
				ans[i].push_back(0);
			}
		}
		m-=1;
		// for (int i  =0; i < n; i++){
		// 	ans[i][0] = m-1;
		// }
		int awf, bawf;
		vector<vector<int>> aww;
		awf = -1;
		bawf = -1;
		for (int i = 0; i < n; i++){
			for (int j = i; j < m; j++){
				ans[i][j] = j-i;
			}
		}
		if (m>4){
			if (true){
			for (int i = 1; i < n; i++){
				for (int j = i-1; j >=0; j--){
					ans[i][j] = m-(m-(m-(j)-1)-1)-1;
					if (ans[i][j] == j+1){
						ans[i][j] += 1;
						//ans[i][j-1] = 10;
						awf = i;
						bawf = j-1;
						aww.push_back({awf,bawf});
					}
				}
			}}
			else{
				for (int i = 1; i < n; i++){
				for (int j = i-1; j >=0; j--){
					ans[i][j] = (m-(m-(j)-1)-1);
				}
				//
			}
			}
				for (int i =0; i < n; i++){
					ans[i][ans[i].size()-1] = ans[i].size()-1;
				}
			for (int i = 1; i < n; i++){
				//ans[i][0] = m-1;
			}
			int ababb = 0;
			if (n > 2){
				//ans[n-1][m-1] = 0;
			}
		}
		else if (m == 4){
			vector<vector<int>> answer = {
				{0, 1, 2, 3, 4},
				{3, 0, 1, 2, 4},
				{2, 3, 0, 1, 4},
				{2, 3, 1, 0, 4},
				{2, 3, 1, 0, 4},
			};
			n = 5;
			for (int i = 0; i < min(dupn,n); i++){
				ans[i] = answer[i];
			}
		}
		else if (m == 3){
			vector<vector<int>> answer = {
				{0, 1, 2, 3},
				{2, 0, 1, 3},
				{2, 1, 0, 3},
				{2, 1, 0, 3},
			};
			n = 4;
			for (int i = 0; i < min(dupn,n); i++){
				ans[i] = answer[i];
			}
		}
		else if (m == 2){
			vector<vector<int>> answer = {
				{1,0,2},
				{0,2,1}
			};
			n = 2;
			ans[0] = answer[0];
			ans[1] = answer[1];
			for (int i =0; i < min(dupn,n); i++){
				ans[i] = answer[i];
			}
		}
		else if (m == 1){
			vector<vector<int>> answer = {
				{0, 1}
			};
			n = 1;
			ans[0] = answer[0];
		}
		else{
			vector<vector<int>> answer = {
				{0}
			};
			n = 1;
			ans[0] = answer[0];
		}
 
		vector<int> model = ans[0];
		for (int i = n; i < dupn; i++){
			ans[i] = model;
		}
		for (int i = 0; i < aww.size(); i++){
			ans[aww[i][0]][aww[i][1]] -= 1;
		}
		//cout << n+1 << "\n";
		//cout << "\n";
		cout << calcbeaut(ans, ans[0].size(), ans.size()-1) << "\n";
		for (int i = 0; i < dupn; i++){
			for (int j = 0; j < ans[i].size(); j++){
				cout << ans[i][j] << " ";
			}
			cout << "\n";
		}
	}
}


Comments

Submit
0 Comments
More Questions

Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game